package com.aivin.myrecyclerviewsample.custom;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.aivin.myrecyclerviewsample.R;
import com.aivin.myrecyclerviewsample.tool.WkLog;

import java.util.Collections;
import java.util.List;


public class MyAdapter extends RecyclerView.Adapter {

    private List<String> datas;
    private RecyclerView recyclerView ;
    private OnAdapterItemClickListener onAdapterItemClickListener ;

    public MyAdapter(List<String> datas ,OnAdapterItemClickListener onAdapterItemClickListener) {
        this.datas = datas;
        this.onAdapterItemClickListener = onAdapterItemClickListener;
    }


    private int creatCount =  0;
    private int bindViewCount =  0;
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        recyclerView = (RecyclerView) parent;
        Context context = parent.getContext();
        View view = null;
        view = LayoutInflater.from(context).inflate(R.layout.item_qq_msg, parent, false);
        creatCount++ ;
        WkLog.showMsg("onCreateViewHolder=" + creatCount);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickView(view);
            }
        });
        return new MyViewHolder(view);
    }



    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        String txtMsg = datas.get(position);
        final MyViewHolder chooseHolder = (MyViewHolder)holder;
        chooseHolder.item_txt.setText(txtMsg);

        bindViewCount++ ;
        WkLog.showMsg("onBindViewHolder=" + bindViewCount);
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }


    public void addOneItem(int position ,String value){
        if(datas==null){
            return ;
        }
        datas.add(position,value) ;
        notifyItemInserted(position);
        if (position == 0) {
            recyclerView.scrollToPosition(0);
        }
    }

    public void removeOnItem(int position){
        if(datas==null){
            return ;
        }
        if(datas.size()<=0){
            return ;
        }
        datas.remove(position) ;
        notifyItemRemoved(position);
    }

    public void itemSwap(int from, int to) {
        Collections.swap(datas, from, to);
        if (to == 0) {
            recyclerView.scrollToPosition(0);
        }
        notifyItemMoved(from, to);//刷新移动数据---将不刷新position
    }

    public void updateItem(int position, String s) {
        datas.set(position, s);
        notifyItemRangeChanged(position, 1);
    }


    private void onClickView(View view) {
        int position = recyclerView.getChildAdapterPosition(view);
        notifyItemChanged(position);

        if(position== RecyclerView.NO_POSITION){
            return;
        }

        notifyItemChanged(position);

        if(onAdapterItemClickListener!=null){
            onAdapterItemClickListener.onItemClick(position);
        }
    }



    public interface OnAdapterItemClickListener{
        void onItemClick(int itemIndex);
    }

    private void test(RecyclerView.Adapter adapter  ,int position ){



    }
}
